<?php

namespace app\api\controller;

use app\common\controller\HomeBase;
use think\Db;
use org\HttpCurl;
use org\Spider;

class Index extends HomeBase {

    public function index() {
        return $this->result([], 1, '成功');
    }

    public function getK() {
        $i = input('id',1);
        $html = HttpCurl::get('http://caipiao.163.com/award/daily_refresh.html', ['gameEn'=>'ssc','selectDate'=>$i]);
        //dump($html);
        $keywordlist = spider::match($html, array('list' => array(
                        // 先是让 spider 去取得(*)对应的结果
                        'cut' => '<div class="lottery-results">(*)<div style="clear:both;"></div>',
                        // 然后让 spider 用正则去匹配对应的列表，美其名曰：表白
                        'pattern' => array(
                            '#data-win-number=(?<key>[^>]*?)">#is',
                            // 什么第一次表白没有成功？好那么我们来第二种表白方式
                            '#data-period="(?<s>[^>]*?)">#is',
                        )
        ) ));
        $s = 0;
        foreach ($keywordlist['list'] as $k=>$v){
                $data[$s]['dates'] = substr($v['key'],-9);
                $data[$s]['number'] = str_replace(' ', '', substr($v['key'],1,9));
                $data[$s]['create_time'] = time();
                $s++;
        }
        $data = array_sort($data, 'dates', 'ase');
        db('pool')->insertAll($data,true);
        dump($data);die;
    }
    /**
     * 获取历史数据
     */
    public function getList() {
        $num = input('num');
        $wei = input('wei');
        $data = db('pool')->order('dates','DESC')->paginate(500);
        $temp = $data->toArray();
        $biao = 1;
        $newdata = [];
        krsort($temp['data']);
        foreach ($temp['data'] as $k=>$v){
            if(strpos(substr($v['number'], 2,5),$num)===false){
                $newdata[$k][0] = $v['dates'];
                $newdata[$k][1] = $biao;
                $newdata[$k][2] = --$biao;
                $newdata[$k][3] = $v['number'];
                $newdata[$k][4] = '';
                $newdata[$k][5] = $newdata[$k][1];
                $newdata[$k][6] = $newdata[$k][2];
                $newdata[$k][7] = '';
                $newdata[$k][8] = '';
                $newdata[$k][9] = '1';
            }else{
                $newdata[$k][0] = $v['dates'];
                $newdata[$k][1] = $biao;
                $biao = $biao+2;
                $newdata[$k][2] = $biao;
                $newdata[$k][3] = $v['number'];
                $newdata[$k][4] = '';
                $newdata[$k][5] = $newdata[$k][1];
                $newdata[$k][6] = $newdata[$k][2];
                $newdata[$k][7] = '';
                $newdata[$k][8] = '';
                $newdata[$k][9] = '1';
            }
        }
        ksort($newdata);
        $temp['data'] = $newdata;
        return $this->result($temp,1,'成功');
    }
}
